package nf.graph;

import java.util.AbstractCollection;
import java.util.Map;
import java.util.HashMap;
import java.util.List;
import java.util.LinkedList;
import java.util.Set;
import java.io.Serializable;

/**
 * Represents an undirected graph.
 */
public class UndirectedGraph extends Graph
{
	static final long serialVersionUID = -5826517012726463461L;
	
	/**
	 * Creates a new empty graph.
	 */
	public UndirectedGraph()
	{
		this(new HashMap<Node, Set<Node>>());
	}
	
	/**
	 * Copies the nodes and edges of an existing graph into a new graph. The
	 * copy is shallow; the node objects themselves are not duplicated.
	 * @param graph	the graph to be copied
	 */
	public UndirectedGraph(Graph graph)
	{
		this(graph.copyAdjacencyMap());
	}
	
	private UndirectedGraph(HashMap<Node, Set<Node>> nodes)
	{
		this.nodes = nodes;
	}
	
	protected Graph createNewInstance()
	{
		return new UndirectedGraph();
	}
	
	public List<Node> getPredecessors(Node node)
	{
		return getNeighbors(node);
	}
	
	public boolean connect(Node from, Node to)
	{
		boolean forward = super.connect(from, to);
		boolean reverse = super.connect(to, from);
		
		return forward || reverse;
	}
	
	public boolean disconnect(Node from, Node to)
	{
		boolean forward = super.disconnect(from, to);
		boolean reverse = super.disconnect(to, from);
		
		return forward || reverse;
	}
}
